var api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var app = getApp();
Page({

  data: {
    machine_id: "",
    showOpen: false,
    showNoOrder: false,
    // 开门成功
    showOpenSuccess: false,

    isReLoad: false,
    // 无操作权限
    showService: false,
    // 数据异常
    showYC: false,
    // 当前开门中
    showOpenDooring: false,
    // 开门需要重试
    showReload: false,
    // 数据盘点
    showDataInventory: false,
    devheight:0,
  },

  onLoad: function (options) {

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          devheight: res.windowHeight,
          // showOpenSuccess: true,
        });
      }

    }); 
// return;



    var mid = options["machineid"];

    if (util.isEmpty(mid)) {
      var page = decodeURIComponent(options.q);
      var strs = page.split("?"); //字符分割 
      var machineids = strs[1];
      var machineid = machineids.split("=")[1];
      this.machine_id = machineid;
    } else {
      this.machine_id = mid;
    }


    if (!util.isEmpty(app.globalData.accountInfo)) {
      // 判断是否具有开门权限
      var accountInof = app.globalData.accountInfo;
      var issu = accountInof.type == 3 || (accountInof.type == 4 && accountInof.is_open == 2)
        if(!issu){
          this.setData({
            showService: true
          })
          return;
        }


      this.openDoor(this.machine_id);
    } else {
      this.autoLogin();

    }




  },
  openDoor: function (e) {
    console.log("openDoor - openDoor");
    var that = this;
    var params = new Object()
    params.machine_id = e;
    api.POST({
      params: params,
      url: app.globalData.shop_openDoor_url,
      success: function (res) {
        console.log(res);
        var code = res.data.code;
        // code = "50003";
        if (code) {
          if (code == '5901') {//无操作权限
            that.setData({
              showService: true
            })
            return;
          } else if (code == "2402" || code == "50006") {//数据异常
            that.setData({
              showDataInventory: true
            })
            setTimeout(function(){
              wx.navigateBack();
            },10000)
            return;
          } else if (code == "50004" || that.data.isReLoad || code == "50001") {//机器故障，不可开门
            that.setData({
              showYC: true,
              showReload: false
            })
            return;
          } else if (code == "50003") {//机器故障，需要重试
            that.setData({
              showReload: true
            })
            return;
          } else if (code == "50005") {//机器处于开门状态
            that.setData({
              showOpenDooring: true
            })
            return;
          }



        } else {
          var accountInof = app.globalData.accountInfo;

          var issu = accountInof.type == 3 || (accountInof.type == 4 && accountInof.is_open == 2)

          that.setData({
            showOpenSuccess: issu
          })




        }
      },
      fail: function (fail) {
        util.showErrorPath("../../../", "请检查网络");
      }
    })
  },
  // 自动登录
  autoLogin: function () {
    var that = this;
    util.showLoading("数据加载中...");

    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code;
          var params = new Object()
          params.code = code;
          api.POST({
            params: params,
            url: app.globalData.login_url,
            success: function (respone) {
              if (!util.isEmpty(respone.data.code) && respone.data.code == '11906') {
                wx.redirectTo({
                  url: '../../../../login/login',
                })

              } else if (!util.isEmpty(respone.data.code) && respone.data.code == '5802') {
                util.showErrorPath("../../../", "密码错误");
              }
              else if (!util.isEmpty(respone.data.code) && respone.data.code == '2499') {
                util.showErrorPath("../../../", "该账号已经绑定过其他微信账号了");
              } else {

                app.globalData.loginState = 1;
                app.globalData.accountInfo = respone.data.data.result;

                that.openDoor(that.machine_id);
                util.hiddenLoading();
                console.log("machineidopenDoor");

              }


            },
            fail: function (fail) {
              console.log("autoLogin = 登录失败");
            }
          })
        }
      },
      fail: function () {
        console.log("wx.login = 登录失败");
        util.showErrorPath("../../../", "数据加载，请检查网络");
      }
    })
  },

  reloadClick: function () {
    this.setData({
      isReLoad: true
    });
    this.openDoor(this.machine_id);
  },
})